<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/AdminIndex' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>产品管理</el-breadcrumb-item>
    <el-breadcrumb-item>产品类别</el-breadcrumb-item>
  </el-breadcrumb>

  <!-- 搜索导航栏 -->
  <div class="con" >
    <div style="float: left">
      <el-button type="primary" icon="el-icon-edit"  @click="addDialogVisible = true">新建</el-button>
      <el-button type="success" icon="el-icon-share">导出</el-button>
    </div>

    <div style="float: right">
      <!-- 搜索添加 -->
      <el-input placeholder="请输入搜索的产品类别" clearable v-model="queryInfo.query"  @clear="getUserList">
        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
      </el-input>
    </div>
  </div>


  <!-- 信息展示栏 -->
  <div class="con">
    <el-table :data="userList" border stripe>
      <el-table-column label="序号"  type="index" width="50px"></el-table-column>
      <el-table-column label="类别ID" prop="product_kind_id"></el-table-column>
      <el-table-column label="类别名称" prop="kind"></el-table-column>
      <el-table-column label="创建时间" prop="pk_create_time"></el-table-column>
      <el-table-column label="修改时间" prop="pk_update_time"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUser(scope.row.product_kind_id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <!-- 分页 size-change 每页最大数变化 current-change 页数变化 layout 功能组件-->
      <el-pagination
          style="text-align: center; "
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 5, 9, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </div>




  </div>

  <!-- 创建新用户对话框 -->
  <el-dialog title="添加种类" :visible.sync="addDialogVisible" width="30%"
             @close="addDialogClosed" >
    <div >
    <a-form-model   :model="form1" :rules="rules1" ref="ruleForm1"  style="text-align: center" >

            <a-form-model-item class="Item inputbox" prop="kind" style="width: 100%" >
              <a-input
                  v-model="form1.kind"
                  placeholder="种类"
                  size="large"
              >
                <a-icon
                    slot="prefix"
                    type="smile"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>
    </a-form-model>
    </div>
    <!-- 内容底部区域 -->
    <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
  </el-dialog>

  <!-- 修改用户对话框 -->
  <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="30%" @colse="editDialogClosed">
    <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">

      <!-- 姓名 -->
      <el-form-item label="种类" prop="kind">
        <el-input v-model="editForm.kind" ></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
  </el-dialog>

</div>
</template>

<script>
export default {
  name: "GoodKind",
  data(){
    return{
      // 请求数据
      queryInfo: {
        query: "",
            pageNum: 1,
            pageSize: 9
      },
      //新建用户
      form1:{
        kind:''
      },
      //修改窗口
      editForm:{
        kind:'',
        product_kind_id:''
      },
      //修改窗口
      editDialogVisible:false,
      editFormRules:{
        kind:[
          { required: true, message: "请输入种类", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
      },
      //判断框
      rules1:{
        kind:[
          { required: true, message: "请输入种类", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
      },
      addDialogVisible:false,
      ban:false,
      choice:true,
      userList:[],
          total:0,
    }

  },
  created() {
    this.getUserList();
  },
  methods:{
      async getUserList(){
        const {data:res} = await this.$http.get("allProductKind",{params:this.queryInfo});
        console.log(res)
        this.userList=res.data;
        this.total=res.number;
      },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.ruleForm1.resetFields();// 重置表单项
      this.choice=true
    },
    // 添加用户
    addUser(){
      this.$refs.ruleForm1.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("addPk",this.form1);
        if (res != "success") {
          return this.$message.error(res);
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getUserList();
      })
    },
    async deleteUser(id){
        const confirmResult =await this.$confirm('产品类别将从数据库中删除，是否继续？','提示',{
          confirmButtonText:'确定',
          cancelButtonText:'取消',
          type:'warning'
        }).catch(err=>err)
      if(confirmResult!='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.delete("/deleteProductKind?id="+id);
      if(res!='success'){
        return this.$message.error(res)
      }else {
        this.getUserList();
        return this.$message.success("删除成功！")

      }
    }
    ,
    editUserInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("updateProductKind",this.editForm);
        if (res != "success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getUserList();
      })
    },
    showEditDialog(user){
      this.editDialogVisible=true
      this.editForm.kind=user.kind
      this.editForm.product_kind_id=user.product_kind_id

    },
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();// 重置表单项
    }
  }
}
</script>

<style scoped>

.con{
  background-color: #FFFFFF;
  margin: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 10px;
  padding-bottom: 10px;
  overflow: hidden;
}

</style>